package com.ruoyi.creditlife.service.util;

import com.ruoyi.creditlife.domain.BookBorrrowRecord;
import com.ruoyi.creditlife.domain.BookInfo;
import com.ruoyi.creditlife.mapper.BookInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;

@Service
public class CaculateAmount {
    @Autowired
    private BookInfoMapper bookInfoMapper;
   public BigDecimal calculateOverdueAmount(BookBorrrowRecord borrrowRecord){
     BookInfo bookInfo= bookInfoMapper.selectBookInfoByBookId(borrrowRecord.getBookId());
     LocalDate dueLocalDate = borrrowRecord.getDueTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
     LocalDate currentDate = LocalDate.now();
     Long days = java.time.temporal.ChronoUnit.DAYS.between(dueLocalDate, currentDate);
     BigDecimal amount = bookInfo.getOverdueFeeRate().multiply(BigDecimal.valueOf(days));
       return  amount;
   }
}
